package com.example.shop_2304.fg

import androidx.lifecycle.viewModelScope
import com.example.base.entity.CarEntity
import com.example.base.entity.PreOrderEntity
import com.example.base.entity.Res
import com.example.base.mvvm.BaseViewModel
import com.example.base.param.PPreOrder
import com.example.base.utils.SingleLiveData
import kotlinx.coroutines.launch

class CarViewModel:BaseViewModel() {
    //购物车列表
    val carSuccess = SingleLiveData<Res<CarEntity>>()
    val carFail = SingleLiveData<String>()
    fun car(isValid:String,limit:String,page:String){
        viewModelScope.launch {
            try {
                carSuccess.postValue(apiService.car(isValid, limit, page))
            } catch (e: Exception) {
                carFail.postValue(e.message.toString())
            }
        }
    }
    //修改购物车数量
    val updateSuccess = SingleLiveData<Res<String?>>()
    val updateFail = SingleLiveData<String>()
    fun updateCar(id:Int,number:Int){
        viewModelScope.launch {
            try {
                updateSuccess.postValue(apiService.updateCar(id, number))
            } catch (e: Exception) {
                updateFail.postValue(e.message.toString())
            }
        }
    }
    //删除购物车
    val deleteSuccess = SingleLiveData<Res<String?>>()
    val deleteFail = SingleLiveData<String>()
    fun deleteCar(ids:Array<Int>){
        viewModelScope.launch {
            try {
                deleteSuccess.postValue(apiService.deleteCar(ids))
            } catch (e: Exception) {
                deleteFail.postValue(e.message.toString())
            }
        }
    }

    //预下单
    val preSuccess = SingleLiveData<Res<PreOrderEntity>>()
    val preFail = SingleLiveData<String>()
    fun preOrder(pPreOrder: PPreOrder){
        viewModelScope.launch {
            try {
                preSuccess.postValue(apiService.preOrder(pPreOrder))
            } catch (e: Exception) {
                preFail.postValue(e.message.toString())
            }
        }
    }
}